﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Class_Entity;
using System.Data;

namespace DataAccess
{
    public class DA__Train
    {
        SqlConnection CON = new SqlConnection();
        SqlCommand CMD;

        public DataSet Load_Train(CE__Train ce)
        {
            DataSet DS = new DataSet();
            try
            {
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();

                CMD = new SqlCommand("spGetAllTrain", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter data = new SqlDataAdapter(CMD);

                CMD.ExecuteNonQuery();
                data.Fill(DS);
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return DS;
        }
        public bool AddNew_Train(CE__Train ce)
        {
            try
            {
                
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();
                CMD = new SqlCommand("spAddNewTrain", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DATA = new SqlDataAdapter(CMD);

                CMD.Parameters.AddWithValue("@TrainName", ce._TrainName);
                CMD.Parameters.AddWithValue("@CategoryID", ce._CategoryID);
                CMD.Parameters.AddWithValue("@Seat", ce._Seat);
                CMD.Parameters.AddWithValue("@TrainTypeID", ce._TrainTypeID);

                CMD.ExecuteNonQuery();
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDatabase.CloseConnection();
            }

            return true;
        }

        public bool Update_Train(CE__Train ce)
        {
            try
            {
                
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();
                CMD = new SqlCommand("spUpdateTrain", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DATA = new SqlDataAdapter(CMD);

                CMD.Parameters.AddWithValue("@TrainID", ce._TrainID); 
                CMD.Parameters.AddWithValue("@TrainName", ce._TrainName);
                CMD.Parameters.AddWithValue("@CategoryID", ce._CategoryID);
                CMD.Parameters.AddWithValue("@Seat", ce._Seat);
                CMD.Parameters.AddWithValue("@TrainTypeID", ce._TrainTypeID);

                CMD.ExecuteNonQuery();
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDatabase.CloseConnection();
            }

            return true;
        }

        public bool Delete_Train(CE__Train ce)
        {
            try
            {
                
                CON = ConnectionToDatabase.StartConnection();
                CON.Open();
                CMD = new SqlCommand("spDeleteTrain", CON);
                CMD.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter DATA = new SqlDataAdapter(CMD);

                CMD.Parameters.AddWithValue("@TrainID", ce._TrainID);

                CMD.ExecuteNonQuery();
                CON.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ConnectionToDatabase.CloseConnection();
            }

            return true;
        }
    }
}
